package com.webapp.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.webapp.model.LoginModel;
import com.webapp.model.Person;
import com.webapp.model.User;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginMooTalkServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginMooTalkServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession session = request.getSession();
		
		String userEmail =  request.getParameter("loginEmail");
		String password = request.getParameter("loginPassword");
		
		LoginModel lm = new LoginModel();
		String result = lm.checkAuthentication(userEmail, password);
		if(result.equals("true")) {
			Person user = (Person)lm.getUser();
			session.setAttribute("UserObject", user);
			System.out.print("test");
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/index.jsp");
			dispatcher.forward(request, response);	
		} else if( result.equals("false")) {
			response.sendRedirect("Login.jsp?login=false");
		} else {
			response.sendRedirect("Error.jsp");
		}
		
	}

}
